package com.bookseek.xreader.service.dk;

import android.database.Cursor;

import com.bookseek.xreader.dbhelper.DbSessionUtils;
import com.bookseek.xreader.utils.DLog;

import org.greenrobot.greendao.database.Database;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class BaseFtsService {
    public Set<Long> getExistRowid(Set<Long> rowids,String tableName){
        Set<Long> results = new HashSet<>();
        if(rowids == null || rowids.size() == 0){
            return results;
        }
        Database db  = DbSessionUtils.genDaoSession().getDatabase();
        StringBuffer sql = new StringBuffer("SELECT rowid FROM ").append(tableName).append(" ");
        sql.append(" WHERE rowid IN ( ");
        String idsStr = rowids.stream().map(d->d.toString()).collect(Collectors.joining(","));
        sql.append(idsStr);
        sql.append(")");
        Cursor cursor = db.rawQuery(sql.toString(), null);
        try{
            // 遍历 Cursor 处理结果
            if (cursor.moveToFirst()) {
                do {
                    Long rowid  = cursor.getLong(cursor.getColumnIndexOrThrow("rowid"));
                    results.add(rowid);
                    // 处理数据...
                } while (cursor.moveToNext());
            }
        }finally {
            if(cursor != null && !cursor.isClosed()){
                DLog.info("getExistRowid 关闭 cursor ");
                cursor.close();
            }
        }
        return results;
    }

}
